﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using PDAMain.DAL;
using PDAMain.Entity;
using PDAMain.Interface;
using OpenNETCF.Net;
using OpenNETCF.Net.NetworkInformation;
using System.Xml;
using System.IO;
using System.Reflection;
using OpenNETCF.Ftp;
using Microsoft.WindowsMobile.Status;
using System.Diagnostics;

namespace PDAMain
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        private void Login_Load(object sender, EventArgs e)
        {
            if (ConfigHelper.InitConfigFile())
            {
                string config = ConfigHelper.GetInfo();
                string[] settings = config.Split(',');
                string Mac = settings[2];
                foreach (INetworkInterface currentInterface in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (currentInterface.Name == Mac)
                    {
                        this.lbldevice.Text = "设备编号：" + currentInterface.GetPhysicalAddress().ToString();
                        lblMAC.Text = currentInterface.GetPhysicalAddress().ToString();
                    }
                }
                //检查本地数据库是否存在，不存在则初始化！
                DataHelper.InitLocalDataBase();
                /*
                string loginedAccount = DataHelper.UserDao.getLoginedUserAccount();

                if (loginedAccount != null)
                {
                    //Identity.dutyPerson = loginedAccount;
                    //Identity.RoleCode = DataHelper.UserDao.getUserRoles(Identity.dutyPerson);
                    //Application.Run(new FunctionList()); 
                    //Application.Run(new FuntionView());
                    //FuntionView Funl = new FuntionView();
                    //FunctionList Funl = new FunctionList();
                    //Funl.Show();
                }*/

                //txtaccount.Text = "testch";
                //txtpassword.Text = "123456";
                //txtaccount.Text = "test";
                //txtpassword.Text = "test123";

            }
            else
            {
                MessageBox.Show("请检查配置文件！");
            }

            
        }

        private void pbLogin_Click(object sender, EventArgs e)
        {
            string webServiceUrl = ConfigHelper.GetConfigValue("Webservice");
            if (webServiceUrl == "")
            {
                Config config = new Config();
                config.Show();
            }
            else
            {
                if (txtaccount.Text.Equals("") || txtpassword.Text.Equals(""))
                {
                    MessageBox.Show("请先输入账号或密码！");
                    return;
                }

                if (NetWorkState.checkWifiState() || NetWorkState.checkGprsState())
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Cursor.Show();
                    string message = DataHelper.UserDao.login(txtaccount.Text.ToString().Trim(), txtpassword.Text.ToString().Trim(), lblMAC.Text.ToString());
                    if (message == "")
                    {
                        Identity.dutyPerson = txtaccount.Text.ToString().Trim();
                        //Identity.RoleCode = DataHelper.UserDao.getUserRoles(Identity.dutyPerson);
                        timer1_Tick(null, null);
                        timer1.Interval = ConfigHelper.GetTime();
                        timer1.Enabled = true;
                        FuntionView Funl = new FuntionView();
                        //FunctionList Funl = new FunctionList();
                        Funl.Show();
                    }
                    else
                    {
                        MessageBox.Show(message);
                        txtaccount.Text = "";
                        txtpassword.Text = "";
                        if (message == "无法连接到远程服务器")
                        {
                            Config config = new Config();
                            config.Show();
                        }
                    }
                    Cursor.Current = Cursors.Default;
                    Cursor.Hide();
                }
                else
                {
                    MessageBox.Show("请检查网络连接！");
                    return;
                }
            }
        }
        
        private void pbReset_Click(object sender, EventArgs e)
        { 
            txtaccount.Text = "";
            txtpassword.Text = "";
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            //未处理任务铃声提醒
            if((Identity.AmNum!="0")||(Identity.WaterNum!="0")||(Identity.RepairNum!="0")||(Identity.CustomerNum!="0")||(Identity.InspectionNum !="0"))
            {
                int ss = 1;
                string FILE_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + @"\Alarm.wav";
                //Sound sound = new Sound("Program Files//pdamain//Alarm.wav");
                Sound sound = new Sound(FILE_PATH);
                sound.Play();
                //SaveExLog("声音提醒第"+ss+"次"+",执行时间："+DateTime.Now);
                ss++;
            }
            //自动同步时数据
            if (NetWorkState.checkWifiState() || NetWorkState.checkGprsState())
            {
                try
                {
                    if (Identity.RoleCode != "")
                    {
                        DataHelper.SynchronizationDataDao.UploadData();
                        string RoleCode = Identity.RoleCode;
                        if (RoleCode.Contains("ROLE_SUPER"))
                        {
                            //系统设置
                            DataHelper.InspectionWorkDao.updateDataFromWebservice();
                            DataHelper.CustomerWorkDao.updateDataFromWebservice();
                            DataHelper.RepairWorkDao.download();
                            DataHelper.ReadingAmmeterDao.download();
                            DataHelper.ReadingWatermeterDao.download();
                        }
                        else
                        {
                            string[] UploadData = RoleCode.Split(',');
                            foreach (string strRole in UploadData)
                            {
                                //维修工单
                                if (strRole.ToUpper() == "ROLE_REPAIRER")
                                {
                                    DataHelper.RepairWorkDao.download();
                                }
                                
                                //巡检管理
                                if (strRole.ToUpper() == "ROLE_INSPECTOR")
                                {
                                    DataHelper.InspectionWorkDao.updateDataFromWebservice();
                                }
                                //客服工单
                                if (strRole.ToUpper() == "ROLE_CUSTOM_SERVICE")
                                {
                                    DataHelper.CustomerWorkDao.updateDataFromWebservice();
                                }

                                //电表抄表
                                if (strRole.ToUpper() == "ROLE_AM_READING" && NetWorkState.checkWifiState())
                                {
                                    DataHelper.ReadingAmmeterDao.download();
                                }

                                //水表抄表
                                if (strRole.ToUpper() == "ROLE_WATER_READING" && NetWorkState.checkWifiState())
                                {
                                    DataHelper.ReadingWatermeterDao.download();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex) { }
            }
        }









        public void SaveExLog(string strErrMessage)
        {
            string OutPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + @"\log.txt";
            if (File.Exists(OutPath) == false)
            {
                StreamWriter sw1 = File.CreateText(OutPath);
                sw1.Flush();
                sw1.Close();
            }
            StreamWriter sw = File.AppendText(OutPath);
            sw.WriteLine(strErrMessage);
            sw.Flush();
            sw.Close();
        

        }

        
        
    }

    
}